<?php
namespace app\system\model;
use app\base\model\BaseModel;
/**
 * 用户操作
 */
class SystemUserModel extends BaseModel {

    //完成
    protected $_auto = array (
        array('username','htmlspecialchars',3,'function'),  //用户名
        array('nicename','htmlspecialchars',3,'function'),  //昵称
        array('email','htmlspecialchars',3,'function'),  //邮箱
        array('password','md5',1,'function'),  //新增时密码
        array('password','',2,'ignore'),  //编辑时密码
        array('status','intval',3,'function'),  //状态
        array('reg_time','time',1,'function'),  //注册时间
     );
    //验证
    protected $_validate = array(
        array('group_id', 'is_empty', '请选择用户组', 1, 'function',3),
        array('username','1,20', '用户名称只能为1~20个字符', 1 ,'length',3),
        array('username', '', '已存在相同的用户名', 1, 'unique',3),
        array('email','email', '邮箱地址输入不正确', 1 ,'regex',3),
        array('email','', '已存在相同的邮箱', 1 ,'unique',3),
        array('password', '4,250', '请输入最少4位密码', 1, 'length',1),
    );

    /**
     * 获取列表
     * @return array 列表
     */
    public function loadList($where = array(), $limit = 0, $order = 'A.user_id desc'){

        $list = $this->table('system_user as A')
            ->join('{pre}system_group as B ON B.group_id = A.group_id')
            ->field('A.* , B.name as group_name')
            ->where((array)$where)
            ->limit($limit)
            ->order($order)
            ->select();
        return $list;
    }

    /**
     * 获取信息
     * @param int $userId ID
     * @return array 信息
     */
    public function getInfo($userId)
    {
        $map = array();
        $map['user_id'] = $userId;
        return $this->getWhereInfo($map);
    }

    /**
     * 获取信息
     * @param array $where 条件
     * @return array 信息
     */
    public function getWhereInfo($where) {
        $info = $this->table('system_user as A')
                ->join('{pre}system_group as B ON A.group_id = B.group_id')
                ->field('A.*,B.status as group_status,B.name as group_name,B.base_purview,B.menu_purview')
                ->where((array)$where)
                ->find();
        if(!empty($info)){
            $purview = target('System/SystemGroup')->loadCrumb($info['group_id']);
            $info = array_merge($info, $purview);
        }
        return $info;
    }

    public function saveUser(){
        $validate = array(
                array('email','email', '邮箱地址输入不正确', 1 ,'regex',2),
                array('email','', '已存在相同的邮箱', 1 ,'unique',2),
                array('password', '4,250', '请输入最少4位密码', 2, 'length',2)
            );
        $auto = array (
                array('nicename','htmlspecialchars',3,'function'),  //昵称
                array('email','htmlspecialchars',3,'function'),  //邮箱
            );
        $data = $this->into('user_id,password,nicename,qq,email,quick')->auto($auto)->validate($validate)->create();
        if(!$data){
            return false;
        }
        if(!empty($data['quick'])){
            $quick = array();
            foreach ($data['quick']['name'] as $key => $value) {
                $quick[$key]['name'] = $value;
                $quick[$key]['icon'] = $data['quick']['icon'][$key];
                $quick[$key]['url'] = $data['quick']['url'][$key];
            }
            
            $data['quick'] = serialize($quick);
        }
        if (!empty($data['password'])){
            $data['password'] = md5($data['password']);
        }else{
            unset($data['password']);
        }
        $status = $this->save($data);
        if($status === false){
            return false;
        }
        return true;

    }

    public function _editBefore($data){
        if(!empty($data['password'])){
            $data['password'] = md5($data['password']);
        }
        return $data;
    }

    /**
     * 登录用户
     * @param int $userId ID
     * @return bool 登录状态
     */
    public function setLogin($userId){
        // 更新登录信息
        $data = array(
            'user_id' => $userId,
            'last_login_time' => time(),
            'last_login_ip' => get_client_ip(),
        );
        $this->save($data);
        //写入系统记录
        api('System','SystemLog','addLog',array('content'=>'登录系统','user_id'=>$userId));
        //设置cookie
        $auth = array(
            'user_id' => $userId,
        );
        session('system_user', $auth);
        session('system_user_sign', data_auth_sign($auth));
        return true;
    }

    /**
     * 注销当前用户
     * @return void
     */
    public function logout(){
        session('system_user', null);
        session('system_user_sign', null);
    }

}
